import 'package:pixelyear/bean/DateBean.dart';
import 'package:pixelyear/bean/StringBean.dart';
import 'package:pixelyear/sql/NoteSqlUtils.dart';
import 'package:pixelyear/sql/Sql.dart';
import 'package:sqflite/sqflite.dart';

class RecordSqlUtils extends Sql {
  RecordSqlUtils._privateConstructor();

  static final RecordSqlUtils _instance = RecordSqlUtils._privateConstructor();

  static RecordSqlUtils get instance => _instance;

  NoteSqlUtils _noteUtils = Sql.getNoteUtils();

  void addData(DateBean bean) async {
    bool exists = await isExists(bean);
    Database db = await getDb();
    if (!exists) {
      await db.rawInsert(
          "insert into " +
              Sql.recordName +
              " (color,year,month,day,textColor,mood) values(?,?,?,?,?,?)",
          [
            bean.color.toString(),
            bean.year.toString(),
            bean.month.toString(),
            bean.day.toString(),
            bean.textColor.toString(),
            bean.mood
          ]);
      await db.close();
    } else {
      db.rawUpdate(
          "update " +
              Sql.recordName +
              " set color=? , textColor=?,mood=? where year=? and month=? and day =? ",
          [
            bean.color.toString(),
            bean.textColor.toString(),
            bean.mood,
            bean.year.toString(),
            bean.month.toString(),
            bean.day.toString()
          ]);
      await db.close();
    }
  }

  Future<List<DateBean>> getData(int year, int month) async {
    Database db = await getDb();
    List<Map<String, dynamic>> cursor = await db.rawQuery(
        "select * from " +
            Sql.recordName +
            " where year = ? and month = ? order by day asc",
        [year.toString(), month.toString()]);
    List<DateBean> list = [];
    for (var map in cursor) {
      var color = map["color"];
      var day = map["day"];
      var textColor = map["textColor"];
      var mood = map["mood"];
      var bean = DateBean(month, year, day, color, textColor, mood);
      bean.note = "";
      StringBean strBean = await _noteUtils.getData("$year-$month-$day");
      if (strBean != null && strBean.data != null) {
        if (strBean.data.length > 30)
          bean.note = strBean.data.substring(0, 30);
        else
          bean.note = strBean.data;
      }
      list.add(bean);
    }

    await db.close();
    return list;
  }

//获取一条数据
  Future<DateBean> getOneData(int year, int month, int day) async {
    Database db = await getDb();
    List<Map<String, dynamic>> cursor = await db.rawQuery(
        "select * from " +
            Sql.recordName +
            " where year = ? and month = ? and day=?",
        [year.toString(), month.toString(), day.toString()]);
    if (cursor.isNotEmpty) {
      var color = cursor[0]["color"];
      var textColor = cursor[0]["textColor"];
      var mood = cursor[0]["mood"];
      var bean = DateBean(month, year, day, color, textColor, mood);
      await db.close();
      return bean;
    }
    await db.close();
    return null;
  }

  Future<bool> isExists(DateBean dateBean) async {
    Database db = await getDb();
    List<Map<String, dynamic>> cursor = await db.rawQuery(
        "select * from " +
            Sql.recordName +
            " where year = ? and month = ? and day = ?",
        [
          dateBean.year.toString() + "",
          dateBean.month.toString() + "",
          dateBean.day.toString() + ""
        ]);
    await db.close();
    return cursor.isNotEmpty;
  }

//删除一条数据
  void delete(int year, int month, int day) async {
    Database db = await getDb();
    db.rawDelete(
        "delete from " +
            Sql.recordName +
            " where year = ? and month = ? and day = ?",
        ["$year", "$month", "$day"]);
    await db.close();
  }
}
